﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary.DAO;
using ClassLibrary.Data;
using System.Data;

public partial class Admin_ucControl_ucMenuParrent : System.Web.UI.UserControl
{
    TblMenuParrentDAO menuPaDAO = new TblMenuParrentDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadMenu();
            string id = Request.QueryString["id"];
            if (Global.ConvertToInt(id) != 0)
            {
                LoadChiTiet();
            }
        }
    }

    private void LoadChiTiet()
    {
        string id = Request.QueryString["id"];
        DataTable dt = menuPaDAO.GetMenuPa_ByID(Global.ConvertToInt(id));
        foreach (DataRow dr in dt.Rows)
        {
            txtMenuParName.Text = dr["MenuParentName"].ToString();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];
            TblMenuParent menuPa = new TblMenuParent();
            menuPa.MenuParentName = txtMenuParName.Text;

            string path = Server.MapPath("~\\Admin\\img\\Items\\");
            string filename = fileImg.FileName;
            if (System.IO.File.Exists(path + filename))
            {

                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = filename.ToString();
            }
            fileImg.SaveAs(path + filename);

            menuPa.ImageURL = filename;


            if (id == "" || id == null)
            {
                menuPaDAO.InsUp(menuPa, 1);
            }
            else
            {
                menuPa.IDMenuParent = Global.ConvertToInt(id);
                menuPaDAO.InsUp(menuPa, 2);
            }

            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            txtMenuParName.Text = string.Empty;
            loadMenu();

        }
    }

    private void loadMenu()
    {
        grdMenuPa.DataSource = menuPaDAO.GetAll();
        grdMenuPa.DataBind();
    }
}